package com.colin.aiyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.provinceEntity;
import com.colin.aiyi.entity.response.ProvinceResponse;
import com.colin.aiyi.utils.SharePreferenceUtil;
import com.colin.aiyi.view.ShowMsgUtil;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hl on 2017/6/7.
 */

public class CityAc extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, Callback.CommonCallback<String> {


    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.text_banner_left)
    TextView textBannerLeft;
    @BindView(R.id.text_banner_title)
    TextView textBannerTitle;
    @BindView(R.id.text_banner_rigth)
    TextView textBannerRigth;
    private MyAdapter myAdapter;
    private String parentId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_province);
        ButterKnife.bind(this);
        textBannerTitle.setText("城市");
        textBannerLeft.setOnClickListener(this);
        parentId = getIntent().getStringExtra("parentId");
        user = SharePreferenceUtil.getObject(this, User.class);
        myAdapter = new MyAdapter(R.layout.simple_list_item_1);
        myAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myAdapter);
        initData();


    }

    void initData() {
        RequestParams params = new RequestParams(Constant.find_city);
        params.addBodyParameter("accessToken", user.getAccessToken());
        params.addBodyParameter("parentId", parentId);
        x.http().get(params, this);
        ShowMsgUtil.showLoadingDialog(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        provinceEntity provinceEntity = (com.colin.aiyi.entity.provinceEntity) adapter.getData().get(position);
        String cid = provinceEntity.getCode();
        String cname = provinceEntity.getName();
//        user.setCityId(cid);
//        user.setCityName(cname);
//        SharePreferenceUtil.saveObject(this,user);
//        new ImpSaveUserInfo().getObject(user);
        Intent intent = new Intent();
        intent.putExtra("cid", cid);
        intent.putExtra("cname", cname);
        setResult(0x686, intent);
        finish();
    }


    @Override
    public void onSuccess(String result) {
        ShowMsgUtil.hideLoadingDialog();
        Gson gson = new Gson();
        ProvinceResponse t = gson.fromJson(result, ProvinceResponse.class);

        if (t instanceof ProvinceResponse) {
            if (((ProvinceResponse) t).getCode() == 200) {
                if (((ProvinceResponse) t).getData() != null && ((ProvinceResponse) t).getData().size() > 0) {
                    myAdapter.getData().clear();
                    myAdapter.getData().addAll(((ProvinceResponse) t).getData());
                    myAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {

    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {

    }


    class MyAdapter extends BaseQuickAdapter<provinceEntity, BaseViewHolder> {
        public MyAdapter(@LayoutRes int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, provinceEntity item) {
            helper.setText(R.id.text1, item.getName());
        }
    }


}
